<?php
    
    include 'database.php';
    
    
    function Search($src, $filter, $page=0){
        $db = new Database();
        $pageSize = 10;
        $dts = array();
        $sql = "select id from article where ";
        $whr = "1";
        $lmt = $pageSize * $page;
        
        $sql = "SELECT SQL_NO_CACHE SQL_CALC_FOUND_ROWS tempId, title, category, city, countView, countPicture, userId, createTime FROM article WHERE ";
        if($src!=""){
            $whr = "content like '%".$src."%'";
        }

        $sql = $sql.$whr." Order by createTime Desc LIMIT ".$lmt.", ".$pageSize;
        $conn = null;
        $rst = $db->Select($sql, array($src), $conn);
        ////// 总记录数 //////
        $dtc = $db->SubSelect($conn, "SELECT FOUND_ROWS() as ct");
        $pages = ceil($dtc[0]['ct'] / $pageSize);

        while($row = $rst->fetch(PDO::FETCH_ASSOC)){
            //$line = array("id"=>0, "content"=>"", "status"=>1, "message"=>"不存在");
            array_push($dts, $row);
//            print_r($row);
//            echo "<br >";
        }  
        return array("pages"=>$pages, "rows"=>$dts);
        //$sql += "SELECT FOUND_ROWS()";
        
//        if (count($rst)==0) {
//            list($ip, $city, $addr, $addrDetail) = $addrs;
//            
//            $sql = "insert into article (content, createTime, tempId, ip, city, addr, addrDetail) ".
//                "values (?, ?, ?, ?, ?, ?, ?)";
//            $param = array($content, date("Y-m-d H:i:s"), $tempId, $ip, $city, $addr, $addrDetail);
//            $artId = $db->Run($sql, $param);
//            
//        }else{
//            $sql = "update article set content=? where tempId=?";
//            $rs = $db->Run($sql, array($content, $tempId));
//            $artId = $rst[0]['id'];
//            cout($artId);
//        }
//        $data = array("id"=>$artId);
//        return $data;
    }
    
    
    function Setting($tempId, $params){
        $db = new Database();
        $deadTime = $params['time'];
        if($params['time']=="-1"){
            $deadTime = null;
        }else{
            $tm = time() + (float)($params['time']) * 86400;
            $deadTime = date("Y-m-d H:i:s", $tm);
        }
        
        
        $sql = "update article set title=?, code=?, category=?, timeLength=?, deadTime=?, passwd=?  where tempId=?";
        $rst = $db->Run($sql, array($params['title'], $params['code'], $params['category'], $params['time'], 
            $deadTime, $params['passwd'], $tempId));
        
//         echo $rst;
        
        
    }



?>